package com.galgram.project.controller;

import java.util.List;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.galgram.common.annotation.Log;
import com.galgram.common.core.controller.BaseController;
import com.galgram.common.core.domain.AjaxResult;
import com.galgram.common.enums.BusinessType;
import com.galgram.project.domain.FxNews;
import com.galgram.project.service.IFxNewsService;
import com.galgram.common.utils.poi.ExcelUtil;
import com.galgram.common.core.page.TableDataInfo;

/**
 * 头条新闻Controller
 *
 * @author galgram
 * @date 2024-11-20
 */
@RestController
@RequestMapping("/project/news")
public class FxNewsController extends BaseController {
    @Autowired
    private IFxNewsService fxNewsService;

    /**
     * 查询头条新闻列表
     */
    @GetMapping("/getList")
    public TableDataInfo getNewsList(FxNews fxNews) {
        startPage();
        List<FxNews> list = fxNewsService.getNewsList(fxNews);
        return getDataTable(list);
    }

    /**
     * 新闻点赞
     */
    @PutMapping("/upvote/{id}")
    public AjaxResult upvoteNews(@PathVariable("id") Long id) {
        FxNews news = fxNewsService.getById(id);
        news.setUpvote(news.getUpvote() + 1);
        fxNewsService.updateById(news);
        return success("已点赞");
    }

    /**
     * 取消点赞
     */
    @PutMapping("/upvoteCancel/{id}")
    public AjaxResult upvoteNewsCancel(@PathVariable("id") Long id) {
        FxNews news = fxNewsService.getById(id);
        news.setUpvote(news.getUpvote() - 1);
        fxNewsService.updateById(news);
        return success("已取消点赞");
    }

    /**
     * 查询头条新闻列表
     */
    @PreAuthorize("@ss.hasPermi('project:news:list')")
    @GetMapping("/list")
    public TableDataInfo list(FxNews fxNews) {
        startPage();
        List<FxNews> list = fxNewsService.selectFxNewsList(fxNews);
        return getDataTable(list);
    }

    /**
     * 获取头条新闻详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:news:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(fxNewsService.getById(id));
    }

    /**
     * 新增头条新闻
     */
    @PreAuthorize("@ss.hasPermi('project:news:add')")
    @Log(title = "头条新闻", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FxNews fxNews) {
        fxNewsService.save(fxNews);
        return success("添加成功");
    }

    /**
     * 修改头条新闻
     */
    @PreAuthorize("@ss.hasPermi('project:news:edit')")
    @Log(title = "头条新闻", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FxNews fxNews) {
        fxNewsService.updateById(fxNews);
        return success("修改成功");
    }

    /**
     * 删除头条新闻
     */
    @PreAuthorize("@ss.hasPermi('project:news:remove')")
    @Log(title = "头条新闻", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        fxNewsService.removeByIds(ids);
        return success("删除成功");
    }


    /**
     * 导出头条新闻列表
     */
    @PreAuthorize("@ss.hasPermi('project:news:export')")
    @Log(title = "头条新闻", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FxNews fxNews) {
        List<FxNews> list = fxNewsService.selectFxNewsList(fxNews);
        ExcelUtil<FxNews> util = new ExcelUtil<FxNews>(FxNews.class);
        util.exportExcel(response, list, "头条新闻数据");
    }

    /**
     * 导入头条新闻列表
     */
    @PreAuthorize("@ss.hasPermi('project:news:import')")
    @Log(title = "头条新闻", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<FxNews> util = new ExcelUtil<FxNews>(FxNews.class);
        List<FxNews> userList = util.importExcel(file.getInputStream());
        fxNewsService.saveBatch(userList);
        return AjaxResult.success("导入成功");
    }

    /**
     * 下载模版
     */
    @PreAuthorize("@ss.hasPermi('project:news:export')")
    @PostMapping("/importTemplate")
    public void exportTemplate(HttpServletResponse response) {
        List<FxNews> list = new ArrayList<FxNews>();
        ExcelUtil<FxNews> util = new ExcelUtil<FxNews>(FxNews.class);
        util.exportExcel(response, list, "头条新闻数据");
    }
}
